﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace IMBL
{
    public class IMBLGeneral
    {

        public string IMGuid()
        {
            // Geef GUID terug
            try
            {
                Guid guid = Guid.NewGuid();
                return guid.ToString();
            }
            catch (Exception e)
            {
                throw e;
            }

        }

        public int GetWeekDaysMonth(DateTime date)
        {
            try
            {
                DateTime startdate = new DateTime(date.Year, date.Month, 1);
                DateTime enddate = startdate.AddMonths(1).AddDays(-1);

                int dagen = getweekdagen(startdate, enddate);
                return dagen;
            }
            catch (Exception e)
            {
                throw e;
            }

        }

        public int GetWeekDaysQuarter(DateTime date)
        {
            try
            {

                int quarter = GetQuarter(date);

                int startmaand = 0;
                int endmaand = 0;

                if (quarter == 1)
                {
                    startmaand = 1;
                    endmaand = 3;
                }
                else
                    if (quarter == 2)
                    {
                        startmaand = 4;
                        endmaand = 6;
                    }
                    else
                        if (quarter == 3)
                        {
                            startmaand = 7;
                            endmaand = 9;
                        }
                        else
                            if (quarter == 4)
                            {
                                startmaand = 10;
                                endmaand = 12;
                            }

                DateTime startdate = new DateTime(date.Year, startmaand, 1);
                DateTime enddate = new DateTime(date.Year, endmaand, 1).AddMonths(1).AddDays(-1);

                int dagen = getweekdagen(startdate, enddate);

                return dagen;
            }
            catch (Exception e)
            {
                throw e;
            }

        }

        public int GetWeekDaysYear(DateTime date)
        {
            try
            {
                DateTime startdate = new DateTime(date.Year, 1, 1);
                DateTime enddate = new DateTime(date.Year, 12, 31);

                int dagen = getweekdagen(startdate, enddate);

                return dagen;
            }
            catch (Exception e)
            {
                throw e;
            }

        }



        public int getweekdagen(DateTime startdate, DateTime enddate)
        {
            try
            {
                int dagen = 0;

                for (DateTime date = startdate; date <= enddate; date = date.AddDays(1))
                {
                    if (date.DayOfWeek == DayOfWeek.Saturday || date.DayOfWeek == DayOfWeek.Sunday)
                    {
                    }
                    else
                        dagen = dagen + 1;
                }

                return dagen;
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public int CalculateYears(DateTime date, DateTime Birthdate)
        {
            try
            {
                int leeftijd = date.Year - Birthdate.Year;
                if (date.Month < Birthdate.Month || (date.Month == Birthdate.Month && date.Day < Birthdate.Day))
                    leeftijd--;

                return leeftijd;

            }

            catch (Exception e)
            {
                throw e;
            }
        }

        public int GetQuarter(DateTime date)
        {
            if (date.Month < 4) return 1;
            if (date.Month < 7) return 2;
            if (date.Month < 10) return 3;
            return 4;

        }
    }

}

